package application;

/**
 * Test cases for MessagePasser in a system of 4 processes.
 * We have included two options in this file: 
 * Command-line interface test and Graphical interface test.
 * 
 * In command-line test, the processes will be in listening mode when started.
 * When all processes are started and ready, press "enter" to start the test.
 * Every process will messages to all processes in the system. 
 * This allow us to test the whole system all at once.
 * 
 * In graphical interface test, a window will be popped up when a process is started
 * In the window, you can specify the destination, message class, message id, and content
 * Every time the send/receive button is clicked, send()/receive() will be called once 
 * This allow us to test the system message by message.
 * 
 * The graphical interface test portion is temporarily commented out in this file.
 * 
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;

import javax.swing.*;


/*
 * Command-line test
 * To start the test, input "java Lab0Test [configuration_file_name]" in the command-line window
 * 
 */
/*
public class Lab0Test extends Thread{
	
	public static void main(String[] args) {
		MessagePasser ins = new MessagePasser(args[0]);
	
		System.out.println ("Press enter when system is ready to send/receive messages");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String command = null;
		try {
			command = br.readLine();
		} catch (IOException ioe) {
			System.out.println("IO Exception to read command");
			System.exit(1);
		}
		
		String myName = ins.getMyName();
		Message message = new Message("alice", "class3","id2","data1 from " + myName);
		ins.send(message);
		message = new Message("bob", "class3", "id3","data2 from " + myName);
		ins.send(message);
		message = new Message("charlie", "class2", "id3","data3 from " + myName);
		ins.send(message);
		message = new Message("delta", "class3", "id4","data4 from " + myName);
		ins.send(message);
		message = new Message("charlie", "class2", "id5","data5 from " + myName);
		ins.send(message);
		message = new Message("charlie", "class1", "id6","data6 from " + myName);
		ins.send(message);
		message = new Message("charlie", "class8", "id7","data7 from " + myName);
		ins.send(message);
		
		System.out.println();
		System.out.println();
		System.out.println();
		
		while (true) {
			ins.receive();
		}
	}
}

*/
/*
 * Test with GUI
 * To start the test, please remove the comment of the code below and comment out the code above 
 * Input "java Lab0Test [configuration_file_name]" in the command-line window
 * 
 */



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import realtime.*;

import messagepasser.*;



public class Lab0Test implements ActionListener{
	public JTextField destField, classField, idField, dataField;
	public JTextArea sendHistArea, recvTextArea;
	private JCheckBox loggable, multicast;
	public JButton sendButton, recvButton;
	private JButton criticalButton;
	private MessagePasser passer;
	private JFrame frame;

	public Lab0Test(String configFileName) {
		
		ClockFactory factory = new ClockFactory();
		ClockService clock = factory.getInstance("Vector");
		
		passer = new MessagePasser(configFileName,clock);
		
		frame = new JFrame(passer.getMyName());
		frame.setSize(700, 560);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel pane = new JPanel();
		
		JLabel label2 = new JLabel("Destination:");
		pane.add(label2);
		destField = new JTextField(12);
		pane.add(destField);
		
		JLabel label3 = new JLabel("Message Class:");
		pane.add(label3);
		classField = new JTextField(12);
		pane.add(classField);
		
		JLabel label4 = new JLabel("Message ID:");
		pane.add(label4);
		idField = new JTextField(12);
		pane.add(idField);
		
		JLabel label5 = new JLabel("Message Payload:");
		pane.add(label5);
		dataField = new JTextField(40);
		pane.add(dataField);
		
		loggable = new JCheckBox("Create log");
		pane.add(loggable);
		loggable.setSelected(false);
		
		multicast = new JCheckBox("Multicast");
		pane.add(multicast);
		multicast.setSelected(false);
		
		JLabel label6 = new JLabel("Message sent history:");
		pane.add(label6);
		sendHistArea = new JTextArea(10, 60);
		sendHistArea.setEditable(false);
		JScrollPane scroller1 = new JScrollPane(sendHistArea);
		pane.add(scroller1);
		
		JLabel label7 = new JLabel("Received messages:");
		pane.add(label7);
		recvTextArea = new JTextArea(10, 60);
		//recvTextArea.setEditable(false);
		JScrollPane scroller2 = new JScrollPane(recvTextArea);
		pane.add(scroller2);
		
		sendButton = new JButton("Send");
		sendButton.addActionListener(this);
		pane.add(sendButton);
		
		recvButton = new JButton("Receive");
		recvButton.addActionListener(this);
		pane.add(recvButton);
		
		criticalButton = new JButton("Access Critical Session");
		criticalButton.addActionListener(this);
		pane.add(criticalButton);
		
		Timer t = new Timer();
		
		SendDocumentState task = new SendDocumentState(this.passer, t, this);
		t.schedule(task, 1000, 1000);
		
		frame.setContentPane(pane);
		frame.setVisible(true);
		
	}
	
	public static void main(String[] args) {
		
		if (args.length != 1) {
			System.out.println("Usage: java Lab0Test config_file_name");
			System.exit(0);
		}
		new Lab0Test(args[0]);
	}
	
	public void actionPerformed(ActionEvent evt) {
		if(evt.getSource() == sendButton) {
			System.out.println("sendButton pressed");
			
			
			String send_warning = passer.send(new TimeStampedMessage(destField.getText(), passer.getMyName(), classField.getText(),
					idField.getText(), dataField.getText(), this.loggable.isSelected(), true));
			
			sendHistArea.append("dest: " + destField.getText() + ",  " 
					+ "class: " + classField.getText() + ",  "
					+ "id: " + idField.getText() + ",  " 
					+ dataField.getText() + "\n");
			
			if (!send_warning.equals("send success")){
				sendHistArea.append("!!!" + send_warning);
			}
			
		} else if (evt.getSource() == recvButton) {
			Message recvMsg = passer.receive();
			
			if (recvMsg != null) {
				recvTextArea.append(recvMsg.getPayload().toString() + "\n");
			} else {
				recvTextArea.append("No deliverable message.\n");
			}
		} else if (evt.getSource() == criticalButton) {
			TimeStampedMessage lockMsg = new TimeStampedMessage(destField.getText(), passer.getMyName(), classField.getText(),
					idField.getText(), dataField.getText(), this.loggable.isSelected(), true);
			lockMsg.setMsgType("lock");
		
		
		}
	}
}

